var server = require('../server')
var db = require('../../db/connect')
var jsonres = require('../../libs/jsonres')
var request = require('request')
var config = require('../../config')
var urlencode = require('urlencode2');


// function timedGetText(cb){
// const http = require("http")
// const url = "http://dev.legendpoker.cn:8088/otherauth?playerid=13882231811&platform=%E5%BE%B7%E6%89%91%E5%9C%88"
// http.get(url,(res)=>{
//     var html ="";
//     res.on("data",(data)=>{
//         html+=data
//     })
//
//     res.on("end",()=>{
//         console.log(html)
//         cb(html)
//     })
//
// }).on("error",(e)=>{
//     console.log(`获取数据失败: ${e.message}`)
// })}
server.get('/api/opponents',function(req,res) {


        //content=JSON.parse(html.toString());
        var mobile = req.query.mobile;
        var platform = req.query.platform;
        var uid = '';
        db.find('accounts', {platform: platform, user: mobile}, function (err, result) {
            if (result.length > 0) {
                uid = result[0].userid;
                //if (uid && uid.length > 0)
                if (1 > 0) {
                    console.log(mobile)
                    console.log(uid)
                    console.log(platform)
                    console.log(urlencode(platform))
                    request({
                        url: "http://dev.legendpoker.cn:8088/otherauth?playerid=13882231811&platform=%E5%BE%B7%E6%89%91%E5%9C%88",
                        json: true
                    }, function (err, httpResponse, body) {
                        if (!err) {
                            res.send(jsonres(200, 'success', body))
                        } else {
                            res.send(jsonres(-1, 'not found any match data', err))
                        }
                    })
                } else {
                    return res.send(jsonres(-1, 'not found any userid2', null))
                }
            } else {
                return res.send(jsonres(-1, 'not found any userid1', null))
            }
        })
    })
